<template>
	<div class="register-wrap">
		<div class="ms-login">
			<div class="ms-title">后台管理系统</div>
			<el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
				<el-form-item prop="useracc">
					<el-input v-model="param.useracc" placeholder="useracc">
						<template #prepend>
							<el-button :icon="User"></el-button>
						</template>
					</el-input>
				</el-form-item>

				<el-form-item prop="nickname">
					<el-input v-model="param.nickname" placeholder="nickname">
						<template #prepend>
							<el-button :icon="User"></el-button>
						</template>
					</el-input>
				</el-form-item>

				<el-form-item prop="password">
					<el-input type="password" placeholder="password" v-model="param.password"
						@keyup.enter="submitForm(register)">
						<template #prepend>
							<el-button :icon="Lock"></el-button>
						</template>
					</el-input>
				</el-form-item>
				<div class="register-btn">
					<el-button type="primary" @click="submitForm(register)">注册</el-button>
				</div>
				<div class="login-btn">
					<el-button type="warning" @click="jumpToLogin">去登录</el-button>
				</div>
				<p class="login-tips">Tips : 注册界面。</p>
			</el-form>
		</div>
	</div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useTagsStore } from '../store/tags';
import { usePermissStore } from '../store/permiss';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import type { FormInstance, FormRules } from 'element-plus';
import { Lock, User } from '@element-plus/icons-vue';
import { registerFunc } from '../api/index'

interface RegisterInfo {
	useracc: string;
	password: string;
	nickname: string;
}
// 创建路由实例
const router = useRouter();
const param = reactive<RegisterInfo>({
	useracc: '',
	password: '',
	nickname: ''
});

const rules: FormRules = {
	useracc: [
		{
			required: true,
			message: '请输入用户名',
			trigger: 'blur'
		}
	],
	password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
};
const permiss = usePermissStore();
const register = ref<FormInstance>();
// 提交表单信息（注册）
const submitForm = (formEl: FormInstance | undefined) => {
	// if (!formEl) return;
	let admin_acc = param.useracc;
	let admin_pwd = param.password;
	let admin_nick_name = param.nickname;
	let admin_create_time = "2023-03-27 16:51:00";
	if (admin_acc.length == 0 || admin_pwd.length == 0 || admin_nick_name.length == 0) {
		ElMessage.error('输入框不能为空。');
	}
	else {
		console.log({ admin_acc, admin_pwd, admin_nick_name, admin_create_time });
		// 调用注册接口
		registerFunc({ admin_acc, admin_pwd, admin_nick_name, admin_create_time }).then(
			content => {
				// console.log(content)
				let { code, msg } = content;
				if (code == 200) {
					// 注册成功
					ElMessage({
						type: 'success',
						message: msg,
					});
					router.push('/login');
				} else {
					ElMessage({
						type: 'warning',
						message: msg,
					});
				}
			}
		).catch(
			err => {
				console.log(err);
			}
		)
	}

	// formEl.validate((valid: boolean) => {
	// 	console.log(valid)
	// 	if (valid) {
	// 		// ElMessage.success('登录成功');
	// 		// localStorage.setItem('ms_useracc', param.useracc);
	// 		// const keys = permiss.defaultList[param.useracc == 'admin' ? 'admin' : 'user'];
	// 		// permiss.handleSet(keys);
	// 		// localStorage.setItem('ms_keys', JSON.stringify(keys));
	// 		// router.push('/');
	// 		// 注册接口
	// 		let admin_acc = param.useracc;
	// 		let admin_pwd = param.password;
	// 		let admin_nick_name = param.nickname;
	// 		console.log({admin_acc,admin_pwd,admin_nick_name});

	// 	} else {
	// 		// ElMessage.error('登录成功');
	// 		return false;
	// 	}
	// });
};

const tags = useTagsStore();
tags.clearTags();

// 跳转登录页
const jumpToLogin = () => {
	router.push({
		path: '/login'
	})
}


</script>

<style scoped>
.register-wrap {
	position: relative;
	width: 100%;
	height: 100%;
	background-image: url(../assets/img/login-bg.jpg);
	background-size: 100%;
}

.ms-title {
	width: 100%;
	line-height: 50px;
	text-align: center;
	font-size: 20px;
	color: #fff;
	border-bottom: 1px solid #ddd;
}

.ms-login {
	position: absolute;
	left: 50%;
	top: 50%;
	width: 350px;
	margin: -190px 0 0 -175px;
	border-radius: 5px;
	background: rgba(255, 255, 255, 0.3);
	overflow: hidden;
}

.ms-content {
	padding: 30px 30px;
}

.login-btn,
.register-btn {
	text-align: center;
}

.login-btn button,
.register-btn button {
	width: 100%;
	height: 36px;
	margin-bottom: 10px;
}

.login-tips {
	font-size: 12px;
	line-height: 30px;
	color: #fff;
}</style>
